unit Checkers.Cell;

interface

uses
  SysUtils,
  VCL.Graphics,
  Dialogs,
  VectorGeometry,
  Classes,
  GLScene,
  OpenGLAdapter,
  GLContext,
  GLRenderContextInfo,
  GLMaterial,
  GLObjects,
  GLSpaceText,
  GLHUDObjects,
  GLWindowsFont,
  Checkers.CellLabel,
  Checkers.BorderPlane;

type
//Providing an isntance of a board's cell
  TCell = class(TGLCube)
  private
    FIsSeletable: Boolean;
//In base variant can be a Byte
    FID: Integer;
    FHighlite: TBorderPlane;
    FHUDText: TCellLabel;
    FIsOnBoard: Boolean;
    FTextPosition: TAffineVector;
    function GetIsSeletable: Boolean;
    procedure SetIsSelectanle(const Value: Boolean);
    function GetID: Integer;
    procedure SetID(const Value: Integer);
    function GetIsHighlited: Boolean;
    procedure SetIsHighlited(const Value: Boolean);
    function GetShowText: Boolean;
    procedure SetShowText(const Value: Boolean);
    function GetText: String;
    procedure SetText(const Value: String);
    procedure SetFont(const Value: TGLWindowsBitmapFont);
    function GetIsReversed: Boolean;
    procedure SetIsReversed(const Value: Boolean);
    function GetHighliteColor: TColor;
    function GetIsOnBoard: Boolean;
    procedure SetIsOnBoard(const Value: Boolean);
  public
    constructor Create(AOwner: TComponent); override;
    procedure DoRender(var ARci: TRenderContextInfo;
      ARenderSelf, ARenderChildren: Boolean); override;
    procedure RenderDecals(var ARci: TRenderContextInfo);
    procedure RenderText(var ARci: TRenderContextInfo);
    procedure Highlite(AColor: TColor);
    procedure ClearHighlite;
    property HighlitePlane: TBorderPlane read FHighlite;
//True for black cells, required for selection method of TCheckersVisualizer
    property IsSelectebale: Boolean read GetIsSeletable write SetIsSelectanle;
    property IsHighlited: Boolean read GetIsHighlited write SetIsHighlited;
    property ID: Integer read GetID write SetID;
    property ShowText: Boolean read GetShowText write SetShowText;
    property Text: String read GetText write SetText;
    property Font: TGLWindowsBitmapFont write SetFont;
//Text orientation
    property IsReversed: Boolean read GetIsReversed write SetIsReversed;
    property HighliteColor: TColor read GetHighliteColor;
//Visibility define
    property IsOnBoard: Boolean read GetIsOnBoard write SetIsOnBoard;
  end;

implementation

{ TCell }

constructor TCell.Create(AOwner: TComponent);
begin
  inherited;
  FIsSeletable := False;
  FIsOnBoard := False;
  Visible := False;
  FHighlite := TBorderPlane.CreateAsChild(Self);
  FHighlite.Direction.SetVector(0, 1, 0);
  FHighlite.Position.Y := 0.006;
  FHighlite.Width := 0.51;
  FHighlite.Height := 0.51;
  FHighlite.Pickable := False;
  FTextPosition := AffineVectorMake(0.13, 0.007, -0.25);
  FHUDText := TCellLabel.CreateAsChild(Self);
  FHUDText.Position.AsAffineVector := FTextPosition;
  FHUDText.Direction.SetVector(0, 1, 0);
  FHUDText.Scale.Scale(0.01);
  IsReversed := False;
  ClearHighlite;
end;

procedure TCell.DoRender(var ARci: TRenderContextInfo; ARenderSelf,
  ARenderChildren: Boolean);
begin
  ARci.GLStates.CallList(GetHandle(ARci));
end;

procedure TCell.RenderDecals(var ARci: TRenderContextInfo);
begin
  FHighlite.Render(ARci);
end;

procedure TCell.RenderText(var ARci: TRenderContextInfo);
begin
  if FHUDText.Visible then
    FHUDText.Render(ARci);
end;

procedure TCell.Highlite(AColor: TColor);
begin
  FHighlite.Visible := True;
  FHighlite.Material.FrontProperties.Diffuse.AsWinColor := AColor;
end;

procedure TCell.ClearHighlite;
begin
  FHighlite.Visible := False;
end;

function TCell.GetHighliteColor: TColor;
begin
  Result := FHighlite.Material.FrontProperties.Diffuse.AsWinColor;
end;

function TCell.GetID: Integer;
begin
  Result := FID;
end;

function TCell.GetIsSeletable: Boolean;
begin
  Result := FIsSeletable;
end;

procedure TCell.SetFont(const Value: TGLWindowsBitmapFont);
begin
  FHUDText.BitmapFont := Value;
  FHUDText.Text := Text;
end;

procedure TCell.SetID(const Value: Integer);
begin
  FID := Value;
end;

function TCell.GetIsHighlited: Boolean;
begin
  Result := FHighlite.Visible;
end;

procedure TCell.SetIsHighlited(const Value: Boolean);
begin
  FHighlite.Visible := Value;
end;

function TCell.GetIsOnBoard: Boolean;
begin
  Result := FIsOnBoard;
end;

procedure TCell.SetIsOnBoard(const Value: Boolean);
begin
  FIsOnBoard := Value;
end;

procedure TCell.SetIsSelectanle(const Value: Boolean);
begin
  FIsSeletable := Value;
  if not FIsSeletable then
    FID := -1;
end;

function TCell.GetShowText: Boolean;
begin
  Result := FHUDText.Visible;
end;

procedure TCell.SetShowText(const Value: Boolean);
begin
  FHUDText.Visible := Value;
end;

function TCell.GetText: String;
begin
  Result := FHUDText.Text;
end;

procedure TCell.SetText(const Value: String);
begin
  FHUDText.Text := Value;
end;

function TCell.GetIsReversed: Boolean;
begin
  Result := FHUDText.RollAngle = -90;
end;

procedure TCell.SetIsReversed(const Value: Boolean);
var
  vReverse: ShortInt;
begin
  vReverse := 1;
  if Value then
    vReverse := -1;
  FHUDText.RollAngle := 90 * vReverse;
  FHUDText.Position.X := FTextPosition[0] * vReverse;
  FHUDText.Position.Z := FTextPosition[2] * -vReverse;
end;

end.
